import axios from "axios";
import store from "@/store";
axios.defaults.baseURL = "/";
// axios.defaults.headers.common["Authorization"] = AUTH_TOKEN;
axios.interceptors.request.use((config: any) => {
  if (config.data instanceof FormData) {
    console.log("文件上传");
    config.headers["Content-Type"] = "multipart/form-data";
  }
  const token = localStorage.getItem("token");
  config.headers["Authorization"] = "Bearer " + token || "";
  store.commit("setLoading", true);
  store.commit("setError", { status: false, message: "" });
  return config;
});

axios.interceptors.response.use(
  (config) => {
    setTimeout(() => {
      store.commit("setLoading", false);
    }, 500);
    return config.data;
  },
  (e) => {
    const { error } = e.response.data;
    const err = {
      status: true,
      message: error,
    };
    store.commit("setError", err);
    store.commit("setLoading", false);
    return Promise.reject(e.response.data);
  }
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default axios as any;
